package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.StockCategory;
import com.asset.salesforce.domain.StockSku;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class StockSkuDaoImpl extends BasicDaoImpl<StockSku>{

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	public List<StockSku> getAllSKUsByEmpIdAndProductId(Integer empId,Integer productId){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from StockSku where  owner.id = :empId And stockProduct.id=:productId");
		query.setParameter("empId", empId);
		query.setParameter("productId", productId);
		List<StockSku>  events = new ArrayList<StockSku>();
		events= query.list();
		if(events.size() >0){
			 return events;
		 }else{
			 return new ArrayList<StockSku>();
		 }
	}

	@SuppressWarnings("unchecked")
	public  StockSku getBySkuName(String name) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockSku where name=  :name");
		 query.setParameter("name", name);
		 List<StockSku>list  = new ArrayList<StockSku>();
		 list = query.list();
		 if(list.size() >0){
			 return list.get(0);
		 }else{
			 return null;
		 }
	
	}

	@SuppressWarnings("unchecked")
	public StockSku getBySkuCode(String code) {
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockSku where code=  :code");
		 query.setParameter("code", code);
		 List<StockSku>list  = new ArrayList<StockSku>();
		 list = query.list();
		 if(list.size() >0){
			 return list.get(0);
		 }else{
			 return null;
		 }
	
	}
	
	@SuppressWarnings("unchecked")
	public List<StockSku> getAllStockSKUsByCompanyId(Employee  employee){
		 Session session = sessionFactory.getCurrentSession();
		 Query query = session.createQuery("from StockSku stockSku where stockSku.owner.company.id = :companyId");
		 query.setParameter("companyId", employee.getCompany().getId());
		 List<StockSku>list  = new ArrayList<StockSku>();
		 list = query.list();
			 return list;
	}
}
